package com.dx.wx.service.impl;

import com.dx.wx.service.DxUserService;
import com.dx.wx.service.DxWorkService;
import com.dx.wx.util.DxUtil;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class DxWorkServiceImpl implements DxWorkService {

    @Autowired
    private DxUserService dxUserService;

    @Override
    public void saveWork(Long phone, Map map) {
        Mapper mapper = dxUserService.findByMobile(String.valueOf(phone));
        //领导人
        Mapper admin = DxUtil.getUserInfo(String.valueOf(phone));
        Map res =  new HashMap();
        res.put("shop_name__c",map.get("name"));
        res.put("user_id__c",mapper.getString("_id"));
        res.put("owner", Arrays.asList(admin.getString("openUserId")));
        res.put("address__c",map.get("location"));
        res.put("address_time__c",map.get("locateTime"));
        res.put("img__c",map.get("img"));
        Map data = DxUtil.createMergeData(res);
        data.put("dataObjectApiName","work_check__c");
        DxUtil.save(data);
    }


    @Override
    public Array listWork(Long phone, Long startDate, Long endDate) {
        Mapper userMapper = dxUserService.findByMobile(String.valueOf(phone));
        Map filter = new HashMap();
        filter.put("field_name","user_id__c");
        filter.put("field_values",userMapper.getString("_id"));
        filter.put("operator","EQ");
        Map data = DxUtil.createSearchData(filter);
        if(startDate!=null&&endDate!=null){
            Map startDateFilter = new HashMap();
            startDateFilter.put("field_name","create_time");
            startDateFilter.put("field_values",startDate);
            startDateFilter.put("operator","GTE");
            Map endDateFilter = new HashMap();
            endDateFilter.put("field_name","create_time");
            endDateFilter.put("field_values",endDate);
            endDateFilter.put("operator","LTE");
            data = DxUtil.createSearchData(filter,startDateFilter,endDateFilter);
        }
        data.put("dataObjectApiName","work_check__c");
        return DxUtil.findSimple(data);
    }
}
